package com.morningstar.authorization.config;

import cn.dev33.satoken.stp.SaTokenInfo;
import com.morningstar.common.result.Result;
import com.morningstar.common.result.ResultCode;
import com.morningstar.system.api.feign.ISystemUserFeign;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;

/**
 * @author Admin
 * 用户登录切面
 */
@Slf4j
@Aspect
@Component
public class LoginControllerAdvice {

    @Resource
    private ISystemUserFeign systemUserFeign;

    @Pointcut("execution(* com.morningstar.authorization.controller.LoginController.login(..))")
    private void loginPointCut() {
    }

    @AfterReturning(value = "loginPointCut()", returning = "result")
    public void loginAfterReturning(JoinPoint joinPoint, Object result) {
        Object[] args = joinPoint.getArgs();
        String methodName = joinPoint.getSignature().getName();
        log.info("Method: {}, Args: {}, Result: {}", methodName, args, result);
        if (result instanceof Result<?> result1) {
            if (result1.getCode() == ResultCode.SUCCESS.getStatus()) {
//                systemUserFeign.userLoginSuccess();
                SaTokenInfo saTokenInfo = (SaTokenInfo) result1.getData();
                //TODO 获取IP
                systemUserFeign.userLoginSuccess(Long.parseLong(saTokenInfo.getLoginId().toString()), "");
                return;
            }

        }
    }
}
